package com.lapelittle.order.service;

import com.lapelittle.framework.model.page.PageRequest;
import com.lapelittle.framework.model.page.PageResult;
import com.lapelittle.order.entity.SaleOrder;
import com.lapelittle.order.mapper.SaleOrderMapper;
import com.lapelittle.order.query.SaleOrderQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class SaleOrderService {
    
    @Autowired
    private SaleOrderMapper orderMapper;
    
    public boolean insert(SaleOrder order) {
        return orderMapper.insert(order) > 0;
    }
    
    public SaleOrder findByOrderNo(String orderNo) {
        return orderMapper.findByOrderNo(orderNo);
    }
    
    public boolean update(SaleOrder order) {
        return orderMapper.update(order) > 0;
    }
    
    public PageResult<SaleOrder> queryPage(SaleOrderQuery query) {
        // 查询总数
        Long total = orderMapper.queryCount(query);
        
        // 如果没有数据，直接返回空结果
        if (total == 0) {
            return PageResult.empty();
        }
        
        // 查询数据列表
        List<SaleOrder> list = orderMapper.queryList(query);
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageNum(query.getPageNum());
        pageRequest.setPageSize(query.getPageSize());
        
        return new PageResult<>(pageRequest, total, list);
    }
    
    public List<SaleOrder> queryList(SaleOrderQuery query) {
        return orderMapper.queryList(query);
    }
} 